(function($){
	$.fn.extend({
		
		formParse: function(options){
			var defaults = {
				data: {},
				errors: {},
				specialFields: {},
				hook: function(){}
			}
			
			for(var i in defaults)
				if(typeof(options[i])=='undefined') options[i]=defaults[i]
			
			return $(this).each(function(){
				var data=options.data;
				var errors = options.errors;
				var fields = options.specialFields;
				for(var i in data){
					var element;
					if(typeof(data[i]=='object'))
						element = $(this).find('[name="'+i+'[]"]');
					else
						element = $(this).find('[name="'+i+'"]');
//					alert(i+'-'+data[i]+'-'+element.attr('type'));
					switch(element.attr('type'))
					{
						case 'file':
							break;
						case 'checkbox':
							element.find('[value=""]').attr('checked',true);
						default:
							element.val(data[i]);
					}							
				}
				var commonError = '';
				for(var i in errors){
					for(var j in fields){
						if(errors[i]!='' && i==fields[j]){
							commonError+='<li>'+errors[i]+'</li>';
							break;
						}
					}
					$(this).find('[name="'+i+'"]').after('<span class="help-inline">'+errors[i]+'</span>');
					$(this).find('[name="'+i+'"]').parent().parent().addClass('error');
				}
				$(this).children(':first').before('<div class="alert alert-error">'+
					'<button data-dismiss="alert" class="close" type="button">×</button>'+
					'<ul>'+commonError+'</ul>'+
					'</div>');
				options.hook();
			})
		}
	});
})(jQuery);

//function initForm(id,options)
//{
//	var errors = options.errors
//	var data = options.data;
//	
//	for(var i in data){
//		var e = $(id+' [name="'+i+'"]')
//		switch(e.attr('type'))
//		{
//			default:
//				e.val(data[i]);
//		}
//	}
//	
//	for(var i in errors){
//		var e = $(id+' [name="'+i+'"]')
//		e.addClass(options.errorclass)
//		
//		if(e.next().html()!='') e.next().replaceWith(errors[i]);
//		else e.after(errors[i]);
//	}
//}